package com.atguigu.guli.service.cms.controller.admin;


import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.service.AdService;
import com.atguigu.guli.service.result.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 广告推荐 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-07
 */
@RestController
@RequestMapping("/admin/cms/ad")

public class AdminAdController {
    @Autowired
    AdService adService;
    //查询数据,分页
    @GetMapping("/page/{pageNum}/{pageSize}")
    public R page(@PathVariable Integer pageNum , @PathVariable Integer pageSize){
        Page<Ad> page = new Page<>(pageNum, pageSize);
        //若果进行分页查询就在这边设置条件
        QueryWrapper<Ad> wrapper = new QueryWrapper<>();
         page = adService.page(page, wrapper);
        return R.ok().data("page",page);
    }
    //删除广告
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable String id){
        adService.removeById(id);
        return R.ok();
    }
    //修改广告
    @PutMapping("/update")
    public R update(@RequestBody Ad ad){
        adService.updateById(ad);
        return R.ok();
    }
    //查询全部
    @GetMapping("/list")
    public R list(){
        List<Ad> list = adService.list();
        return R.ok().data("items", list);
    }

    //根据id进行查询
    @GetMapping("/get/{id}")
    public R get(@PathVariable String id){
        Ad ad = adService.getById(id);
        return R.ok().data("item", ad);
    }

    //新增广告
    @PostMapping("/save")
    public R save(@RequestBody Ad ad){
        adService.save(ad);
        return R.ok();
    }
}

